<html>
  <head><title>DriverSync Client</title></head>
  <script type="text/javascript" src="common.js"></script>
  <script type="text/javascript" src="log.js"></script>
  <script type="text/javascript">
   var requestType=0;
   var value="";
   var timeout="";
   var syncaddress="127.0.0.1";
   var syncport="3223";
   var php_sync_script="sync.php";
   var php_write_log_script="writeData.php";
   var php_read_log_script="readData.php";
   var curURL = window.location.pathname;
   var prefix = curURL.substr(0,curURL.lastIndexOf('/')+1);
   var logfile = "SyncClient.log";
   
   function processType(input) {
     switch(input.value) {
       case ("1"):
       requestType=1;
       document.getElementById("setValue").setAttribute("disabled","true");
       document.getElementById("waitValue").setAttribute("disabled","true");
       document.getElementById("waitTimeout").setAttribute("disabled","true");
       break;
       case ("2"):
       requestType=2;
       document.getElementById("setValue").removeAttribute("disabled");
       document.getElementById("waitValue").setAttribute("disabled","true");
       document.getElementById("waitTimeout").setAttribute("disabled","true");
       break;
       case ("3"):
       requestType=3;
       document.getElementById("waitValue").removeAttribute("disabled");
       document.getElementById("waitTimeout").removeAttribute("disabled");
       document.getElementById("setValue").setAttribute("disabled","true");
       break;
       case ("4"):
       requestType=4;
       document.getElementById("setValue").setAttribute("disabled","true");
       document.getElementById("waitValue").setAttribute("disabled","true");
       document.getElementById("waitTimeout").setAttribute("disabled","true");
       break;
       case ("5"):
       requestType=5;
       document.getElementById("setValue").setAttribute("disabled","true");
       document.getElementById("waitValue").setAttribute("disabled","true");
       document.getElementById("waitTimeout").setAttribute("disabled","true");
       break;
       case ("6"):
       requestType=6;
       document.getElementById("setValue").setAttribute("disabled","true");
       document.getElementById("waitValue").setAttribute("disabled","true");
       document.getElementById("waitTimeout").setAttribute("disabled","true");
       break;
     }
   }
   function request() {
     // Build the request
     var request="";
     var varName=document.getElementById("varName").value;
     if (requestType != 4 && varName == "") {
       alert("A variable name is required");
       return -1;
     }
     switch(requestType){
       case (1):
         request=requestType + ' ' + varName;
         break;
       case (2):
         var setValue=document.getElementById("setValue").value;
         if (setValue == "") {
           alert("A value is required for a SET request");
           return -1;
         }
         request=requestType + ' ' + varName + ' ' + setValue;
         break;
       case (3):
         var waitValue=document.getElementById("waitValue").value;
         var waitTimeout=document.getElementById("waitTimeout").value;
         if (waitValue == "") {
           alert("A value is required for a WAIT request");
           return -1;
         }
         if (waitTimeout == "") {
           alert("A timeout is required for a WAIT request");
           return -1;
         }
         request=requestType + ' ' + varName + ' ' + waitValue + ' ' + waitTimeout;
         break; 
       case (4):
         request=requestType;
         break;
       case (5):
         request=requestType + ' ' + varName;
         break;          
       case (6):
         request=requestType + ' ' + varName;
         break;         
     }     
     requestSync(logfile,request,function () { writeLog(logfile,'INFO: onComplete called'); });
   }   
 </script>
  <body>
     <h1>DriverSync Client</h1>
     <hr>
    <fieldset>
      <legend>Request details</legend>
      Variable name: <input id="varName" type="text" name="varName"></input><br>
      Request type: <br>
      <input id="requestType1" type="radio" onClick="processType(this)" name="requestType" value="1">INIT</input><br>
      <input id="requestType2" type="radio" onClick="processType(this)" name="requestType" value="2">SET  Value: <input type="text" disabled="true" id="setValue" size="2" value=""></input></input><br>
     
      <input id="requestType3" type="radio" onClick="processType(this)" name="requestType" value="3">WAIT Value: <input type="text" disabled="true" id="waitValue" size="2" value=""></input> Timeout (milliseconds): <input input type="text" disabled="true" id="waitTimeout" size="5" value=""></input></input><br>
      <input id="requestType4" type="radio" onClick="processType(this)" name="requestType" value="4">RESET ALL VARIABLES</input><br>
      <input id="requestType5" type="radio" onClick="processType(this)" name="requestType" value="5">DELETE</input><br>
      <input id="requestType6" type="radio" onClick="processType(this)" name="requestType" value="6">GET</input><br>
     </fieldset>
      <input type="button" onClick="request()" name="submitRequest" value="Submit request"></input>
  </body>
</html>